#ifndef  __HAL_SPI_H__
#define  __HAL_SPI_H__

//----------------------------------------------------------------------------------
//   Common Macros
//----------------------------------------------------------------------------------
#define HAL_SPI_CS_DEASSERT  MCU_IO_SET(SPI_CSN_PORT, SPI_CSN_PIN)
#define HAL_SPI_CS_ASSERT    MCU_IO_CLR(SPI_CSN_PORT, SPI_CSN_PIN)
#define HAL_SPI_SOMI_VAL     MCU_IO_GET(SPI_MISO_PORT, SPI_MISO_PIN)
#define HAL_SPI_BEGIN        st(HAL_SPI_CS_ASSERT; while(HAL_SPI_SOMI_VAL);)
#define HAL_SPI_END          st(HAL_SPI_CS_DEASSERT;)

//#define HAL_CHIP_READY       st(MCU_IO_GET(SPI_MISO_PORT, SPI_MISO_PIN);)
#define HAL_SPI_WAIT_RXFIN   st(while (!(SPSR & (1<< SPIF)));)
#define HAL_SPI_WAIT_TXFIN   st(while (!(SPSR & (1<< SPIF)));)
#define HAL_SPI_WAIT_TXBUF   st(while (!(SPSR & (1<< SPIF)));)
#define HAL_SPI_RXBUF        SPDR
#define HAL_SPI_TXBUF        SPDR
#define HAL_SPI_TXBUF_SET(x) st(SPDR = (x);)


void   halSpiInit(INT8U clockrate);
INT8U  halSpiWrite(INT8U addr, const INT8U* data, INT8U length);
INT8U  halSpiRead(INT8U addr, INT8U* data, INT8U length);
INT8U  halSpiStrobe(INT8U cmd);

#endif

